﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace AzureBlogWebRole.widgets.ChannelActive
{
    public partial class widget : WidgetBase 
    {
        public override string Name
        {
            get { return "ChannelActive"; }
        }

        public override bool IsEditable
        {
            get { return true; }
        }

        public override void LoadWidget()
        {
            // Nothing to load
        }

        private Guid _ChannelId;

        public Guid ChannelId
        {
            get {
                StringDictionary settings = GetSettings();
                if (settings.ContainsKey("SensorActiveChannel"))
                {
                    _ChannelId = new Guid(settings["SensorActiveChannel"]);
                }
                return _ChannelId; 
            }
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Title = "Le canal du moment" + "&nbsp;&nbsp;<a href=\"/rsschannel.ashx?channelid=" + ChannelId + "\"><img src=\"/pics/rssButton.gif\" /></a>";
        }

        protected void Literal1_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<AzureProvider.MiniSensor.Message> msgs;
                //if (HttpRuntime.Cache["widget_channelsactive"] == null)
                //{
                AzureProvider.MiniSensor.Messages msg_get = new AzureProvider.MiniSensor.Messages();
                msgs = msg_get.GetMessages(ChannelId).Take(10).ToList();

                //}
                foreach (var item in msgs)
                {
                    ((Literal)sender).Text += item.DateCreated.ToShortTimeString() + " " + item.Author + " : " + item.Content + " <br/>";
                }
            }
        }
    }
}